<?php
	require(dirname(dirname(__FILE__))) . '/includes/init.php';
//	require(dirname(__FILE__)) . '/includes/cls_template.php';
//	require(dirname(__FILE__)) . '/includes/client.php';
//	require(dirname(__FILE__)) . '/includes/urlList.php';
//	require(dirname(__FILE__)) . '/includes/serverList.php';
//	require(dirname(__FILE__)) . '/includes/action.php';
//	require(dirname(__FILE__)) . '/includes/class.phpmailer.php';
	require(dirname(__FILE__)) . '/includes/ipFilter.php';
//    date_default_timezone_set('PRC');
//
//    $GLOBALS['db'] = new mysqls("send");
  
	$emailArr = array("yahooId","aolId","hotId","gmailId","otherId");
//	$emailArr = array("testId");
function formatContents($unsubUrlArr,$userActionArr,$content,$id,$type,$templateId){
	$ip = "";$url = '';
	$userBrowser = "";
	$formateDate = date("Y-m-d H:i:s", time());
//		$url = $unsubUrlArr[0];
	$r = rand(1,5);
	$rStr = create_str($r);
	$url = "".$rStr.".china-interview.com";
	
	$unsubUrl = "http://".$url."/subscribe/unsub.php?unsub=".base64_encode($id."|".$type."|".$templateId);
	$trackUrl = "<img width=\"1\" height=\"1\" src=http://".$url."/i.php?m=".base64_encode($id."|".$type."|".$templateId."|").".jpg>";
	$updateUrl = "http://".$url."/subscribe/update.php?update=".base64_encode($id."|".$type."|".$templateId);
	$content = str_replace($userActionArr['unsub'],$unsubUrl, $content);
	$content = str_replace($userActionArr['track'],$trackUrl, $content);
	$content = str_replace($userActionArr['update'],$updateUrl, $content);
	$content = str_replace($userActionArr['email'],$id."&type=".$type, $content);

	$content = addslashes($content);
    $content = "<div align=center>".$content."</div>";
	$content = $content."<br/><table width=\"100%\" border=\"0\" bgcolor=\"#999999\">
  <tr>
    <td><div align=\"center\">
      <table width=\"600\" border=\"0\" bgcolor=\"#666666\">
        <tr>
          <td><p>Did someone forward you this email? Sign up with <a href=\"http://".$rStr.".china-interview.com/subscribe/update.php\" target=\"_blank\">US</a> <br />
            Get the latest on style, care, couponing… <br />
			If you have any question, <a href=\"http://".$rStr.".china-interview.com/idz/\" target=\"_blank\">don\'t hesitate to contact with us</a>.
            </p>
            <p>Adjust your email <a href=\"http://".$rStr.".china-interview.com/subscribe/update.php\" target=\"_blank\">preferences</a> or <a href=\"http://".$rStr.".china-interview.com/subscribe/unsub.php\" target=\"_blank\">unsubscribe</a></p></td>
        </tr>
      </table>
    </div></td>
  </tr>
</table>";
	return $content;
}

function send_simple_messages($title,$content,$sender) {
	$title = trim($title);
	$content = trim($content);
	$emailStr = $sender['address'];
	$emailArr = explode("|",$emailStr);
	if(empty($title)||empty($content)){
		exit("ko");
	}
//	echo "processing...001<br/>";
    $mail = new PHPMailer();
    $mail->IsHTML(true);
    $mail->Charset="utf-8";
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;
    $mail->SMTPAuth = true;
    if(array_key_exists("tls",$sender['smtp'])){
		$mail->SMTPSecure = 'tls';
		$mail->SetFrom($sender['sender'], $sender['senderName']);
	}else{
		$mail->SetFrom($sender['sender'], $sender['senderName']);
	}
//	echo "processing...002<br/>";
//	echo $sender['sender']."---".$sender['senderPWD']."<br/>";
    $mail->Host =  $sender['smtp']['host'];
    $mail->Port = $sender['smtp']['port'];
    $mail->Username = $sender['sender'];
    $mail->Password = $sender['senderPWD'];    
    $mail->Subject = $title;
    $mail->Body = $content;
    $mail->AddAddress($emailArr[0]);
	array_shift($emailArr);

    foreach ($emailArr as $key => $value) {
    	$mail->AddBCC($value);
    }
//	echo "processing...004<br/>";
    #echo $email;
    if(!$mail->Send()){echo $error = 'Mail error: '.$mail->ErrorInfo;;exit;}else{echo "success!".date("Y-m-d H:i:s",time());}
}

/*生成随机字符串*/
function create_str($length) {  
	$chars = 'abcdefghijklmnopqrstuvwxyz';  
	$str = "";  
	for ($i = 0; $i <$length; $i++)  
	{  
		$str .= $chars[mt_rand(0, strlen($chars) - 1)];
	}  
	return $str;  
}
/*生成随机字符串*/

/*单个IP或者整段IP的URL转向*/
function ipLimit($ip,$mode,$ipArr){
	$flag = '';$ipSegmentArr = array();
	if($mode == "single"){
		$ipStr = trim(".",$ip);
		if(in_array($ipStr,$ipArr)){
			$flag = TRUE;
		}else{
			$flag = FALSE;
		}
	}else if($mode == "segment"){
		$tempArr = explode(".",$ip);
		if(empty($tempArr)){
			$tempArr = '';
			$flag = FALSE;
		}else{
			$tempIpStr = $tempArr[0].$tempArr[1].$tempArr[2];
			if(in_array($tempIpStr, $ipArr)){
				$flag = TRUE;
			}else{
				$flag = FALSE;
			}
		}
	}
	return $flag;
}
/*单个IP或者整段IP*/
?>
